/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utilities;

import utilities.entities.Result;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 *
 * @author olawale
// */
public class Store {

    private static File folder = new File("store");

    public static void store(Result sresult){
        if(!folder.exists()){
            folder.mkdir();
        }
        File file = new File(folder, sresult.fileName);
        try{
            FileOutputStream fs = new FileOutputStream(file);
            ObjectOutputStream os = new ObjectOutputStream(fs);
            os.writeObject(sresult);
            os.close();
        }catch(Exception ex){
            System.err.println(ex);
        }
    }

    public static Result retrieve(String fileName){
        Result c = null;
        try{
            FileInputStream fis = new FileInputStream(new File(folder, fileName));
            ObjectInputStream ois = new ObjectInputStream(fis);
            c = (Result) ois.readObject();
            ois.close();
        }catch(Exception ex){
            System.err.println(ex);
        }
        return c;
    }

    public static boolean delete(String fileName){
        try{
         File file = new File(folder, fileName);
         return file.delete();
        }catch(Exception ex){
            System.err.println(ex);
        }
        return false;
    }

    public static boolean exists(String fileName){
        try{
         File file = new File(folder, fileName);
         return file.exists();
        }catch(Exception ex){
            System.err.println(ex);
        }
        return false;
    }

    public static ArrayList<Result> getAll(){
        File [] files = folder.listFiles();
        ArrayList<Result> results = new ArrayList();
        for(File file : files){
            if(file.getName().contains("PR"))
                results.add(retrieve(file.getName()));
        }
        return results;
    }
}
